﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using BLL.Installer;
using WayCloud.Plumbing;
using WayCloud.Utility;

namespace WayCloud
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            #region Menu
            routes.MapRoute(
                "ReOrder",
                "menu/reorder/{id1}|{id2}",
                new { controller = "Menu", action = "ReOrder" },
                new { id1 = @"\d*", id2 = @"\d*" });

            routes.MapRoute(
                "FindMenuInfoById",
                "menu/find/{id}",
                new { controller = "Menu", action = "FindMenuInfoById" },
                new { id = @"\d*" }
                );
            #endregion

            #region User
            routes.MapRoute(
                "FindUserInfoById",
                "user/find/{id}",
                new { controller = "User", action = "FindUserInfoById", id = UrlParameter.Optional }
            );
            #endregion


            #region Apply
            routes.MapRoute(
                "FindApplyInfoById",
                "apply/find/{id}",
                new { controller = "Apply", action = "FindApplyInfoById", id = UrlParameter.Optional }
            );
            #endregion

            #region OperatingSystem
            routes.MapRoute(
                "FindOperatingSystemInfoById",
                "os/find/{id}",
                new { controller = "OperatingSystem", action = "FindOperatingSystemInfoById", id = UrlParameter.Optional }
            );
            routes.MapRoute(
                "OperatingSystem", // 路由名称
                "os/{action}/{id}", // 带有参数的 URL
                new { controller = "OperatingSystem", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
            #endregion

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Account", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            InitializeContainer();
            PlumbingCustomControllerFactory();

            ModelBinders.Binders.DefaultBinder = new CustomModelBinder();

            //如果要测试路由则注释掉下面这行
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }

        //注入MVC的Controller
        private static void PlumbingCustomControllerFactory()
        {
            ContainerHelper.Instance.Container.Install(new ControllersInstaller());
            var controllerFactory = new WindsorControllerFactory(ContainerHelper.Instance.Container.Kernel);
            //指定MVC的controllerFactory
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        }

        //初始化Windsor
        private static void InitializeContainer()
        {
            try
            {
                ContainerHelper.BootstrapContainer();
            }
            catch (Exception e)
            {
                throw new Exception("初始化windsor出错，请检查所有installer后缀名的文件！", e);
            }
        }

        //释放资源
        protected void Application_End()
        {
            ContainerHelper.DisposeContainer();
        }

    }
}